import {Component, OnInit} from '@angular/core';
import {Router} from '@angular/router';
import {EmitService} from '../../providers/emit.service';


@Component({
  selector: 'app-sys-layout',
  templateUrl: './sys.component.html',
  styleUrls: ['./sys.component.css']
})
export class SysComponent implements OnInit {

  isCollapsed = false;
  tabs: any[] = [];
  nzTabPosition = 'top';
  selectedIndex = 0;

  constructor(public emitService: EmitService,private router:Router) {
  }

  ngOnInit() {

   this.tabs.push({
      name: '首页',
      url: '/sys/welcome',
      closeIcon: false,
    });


// 接收发射过来的数据
    this.emitService.tabsEmit.subscribe((value: any) => {
      if (value != null) {
        // 这里就可以调取接口，刷新userList列表数据
        let flag = true;
        let havedIndex;
        let index = 0;
        let o;
        for (var i=0;i<this.tabs.length;i++) {
          o = this.tabs[i];
          if (o.name == value.name && o.url == value.url) {
            flag = false;
            havedIndex =i;
          }
        }
        if (flag) {
          this.tabs.push(value);
          index = this.tabs.length-1;
        }else{
          index = havedIndex;
        }
        setTimeout(() => {
          this.selectedIndex = index;
        }, 500);

      }
    });

  }

  closeTab(tab: string): void {
    this.tabs.splice(this.tabs.indexOf(tab), 1);
  }

  log(args: any[]): void {
   // console.log('selectedIndex=' + this.selectedIndex);
  }

  selectChange(){
    var tab = this.tabs[this.selectedIndex];
    this.router.navigateByUrl(tab.url);
  }

}
